/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship;

import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author Tomek
 */
public class GameSteer {
	static protected MainFrame parent;
	static protected Rules rules;
	static protected Player player1, player2;
	static protected GameCreatorPanel gcp;
	static protected JTable shipsInGameP1Table, shipsInGameP2Table;
	static protected NewGameSettings settings;
	static protected SetShipFieldActionListener sSFAL = new SetShipFieldActionListener();
	private static KeyAdapter keyAdapter = new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent evt) {
					switch(evt.getKeyCode()){
						case KeyEvent.VK_ENTER:{
							if(gcp.doneButton.isEnabled())
								gcp.doneButton.doClick();
							break;
						}
					}
      }
    };

	static protected void setThings(MainFrame p) {
		parent = p;
		rules = p.rules;
		parent.setSize(260+30*rules.xSize, (rules.ySize<13?450:80+rules.ySize*30));
		settings = p.settings;
		player1 = rules.player1;
		player1.table = new MyTableModel(rules.shipsInGame);
		player2 = rules.player2;
		player2.table = new MyTableModel(rules.shipsInGame);
		player1.grid = new Grid(rules.ySize, rules.xSize);
		player2.grid = new Grid(rules.ySize, rules.xSize);
		shipsInGameP1Table = new JTable(new MyTableModel(player1.table));
		sSFAL.curvedShipsAllowed = rules.curvedShipsAllowed;
	}
	public static void setGrid(Grid gr){
			for(Field f :gr){
				f.addActionListener(sSFAL);
				f.addKeyListener(keyAdapter);
			}
			gr.setButtonsEnabled(true);
			gr.setBounds(10, 10, 30*rules.xSize, 30*rules.ySize);
	}
	protected static void gameCreator(){
			parent.gamePhase = GamePhase.CREATING_PLAYER1;
		/*
		//TEST
		setGrid(player1.grid);
		player1.setShips(new MyTableModel(rules.shipsInGame));
		nextPlayerSetsHisShips();
//END TEST*/
		setGrid(player1.grid);
		gcp = new GameCreatorPanel(shipsInGameP1Table, parent, player1);
		sSFAL.doneButton = gcp.doneButton;
		sSFAL.set.clear();
		parent.GameBoard.add(player1.grid);

		parent.infoPanel.setLayout(new FlowLayout());
		parent.infoPanel.add(gcp);

		parent.validateAndRepaintAll();
		JOptionPane.showMessageDialog(parent, "Waiting for "
				+ player1.getName() + "...", "Setting ships...",
				JOptionPane.INFORMATION_MESSAGE);
	}
	static void nextPlayerSetsHisShips() {
			parent.gamePhase = GamePhase.CREATING_PLAYER2;
		if(player2 instanceof HumanPlayer){
			/*//TEST
			parent.clearValidateAndRepaintAll();
			player2.setShips(new MyTableModel(rules.shipsInGame));
			Battleship battle = new Battleship(parent, parent.rules);
			battle.startGame();
			//END TEST*/
			
			shipsInGameP2Table = new JTable(new MyTableModel(rules.shipsInGame));
			parent.clearValidateAndRepaintAll();
			gcp = new GameCreatorPanel(shipsInGameP2Table, parent, player2);
			gcp.actualPlayer = player2;
			sSFAL.doneButton = gcp.doneButton;
			setGrid(player2.grid);
			parent.GameBoard.add(player2.grid);
			parent.infoPanel.setLayout(new FlowLayout());
			parent.infoPanel.add(gcp);
			parent.validateAndRepaintAll();
			JOptionPane.showMessageDialog(parent, "Waiting for "
				+ player2.getName() + "...", "player changing...",
				JOptionPane.INFORMATION_MESSAGE);
		}else{
			ComputerPlayer comp = (ComputerPlayer)player2;
			try{
			comp.setShips(rules.shipsInGame);
			}catch(Exception e){}
			parent.clearValidateAndRepaintAll();
			Battleship battle = new Battleship(parent, parent.rules);
			battle.startGame();
		}
	}

}
